package com.clearance.mine_clearance.controller;
import com.clearance.mine_clearance.entry.BetInfo;
import com.clearance.mine_clearance.entry.Result;
import com.clearance.mine_clearance.entry.dto.BetInfoDto;
import com.clearance.mine_clearance.entry.dto.RedPacketDto;
import com.clearance.mine_clearance.entry.vo.RedPacketVo;
import com.clearance.mine_clearance.mapper.BetInfoMapper;
import com.clearance.mine_clearance.mapper.RedPacketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin("*")
@RestController
@RequestMapping("/mine/redPacket")
public class RedPacketController {

    @Autowired
    private RedPacketMapper redPacketMapper;


    @Autowired
    private BetInfoMapper betInfoMapper;

    @PostMapping("/selectPage")
    public Result<Map<String, Object>> selectPage(@RequestBody RedPacketVo redPacketVo){
        if(redPacketVo.getCurrent()==0){
            redPacketVo.setCurrent(1);
        }
        int firstIndex = (redPacketVo.getCurrent()-1) * redPacketVo.getSize();
        //到第几条数据结束
        int lastIndex = redPacketVo.getCurrent() * redPacketVo.getSize();
        redPacketVo.setCurrent(firstIndex);
        redPacketVo.setSize(lastIndex);
        Integer count = redPacketMapper.count(redPacketVo);
        List<RedPacketDto> redPacketDtos = redPacketMapper.selectPage(redPacketVo);
        for (RedPacketDto redPacketDto : redPacketDtos) {
            List<BetInfoDto> betInfoDtoList = betInfoMapper.selectByRedPacketId(redPacketDto.getId());
            redPacketDto.setBetInfoDtoList(betInfoDtoList);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("count",count);
        result.put("redPacketList",redPacketDtos);
        return new Result<Map<String, Object>>().success(result);
    }
}
















